package com.kinghood.authoritycenter.service.impl;

import com.kinghood.authoritycenter.common.config.TokenProvider;
import com.kinghood.authoritycenter.common.model.dao.auth.UserBaseDO;
import com.kinghood.authoritycenter.common.util.UserUtil;
import com.kinghood.authoritycenter.manager.IOauthManager;
import com.kinghood.authoritycenter.service.IOauthService;
import com.kinghood.authoritycenter.service.IUserBaseService;
import com.kinghood.infrastructure.model.KingHoodExceptionUtil;
import com.kinghood.authoritycenter.common.model.dto.authority.AuthByUserDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.CheckTokenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.GetUserReqDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.UserResDTO;
import com.kinghood.authoritycenter.common.model.dto.auth.KingHoodPrincipal;
import com.kinghood.authoritycenter.common.model.dto.auth.UserDTO;
import com.kinghood.redis.util.RedisManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

/**
 * @author zhangFanJun
 * @date 2023-12-06 23:27
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class OauthServiceImp implements IOauthService {

    private final TokenProvider tokenProvider;
    private final RedisManager redisManager;
    private final IUserBaseService userBaseService;
    private final IOauthManager oauthManager;

    @Override
    public Boolean validateToken(CheckTokenReqDTO in) {
        return tokenProvider.validateToken(in.getToken());
    }

    @Override
    public UserDTO userDetail( ) {

        KingHoodPrincipal kingHoodPrincipal = UserUtil.get();
        KingHoodExceptionUtil.isNull(kingHoodPrincipal,"token异常");
        KingHoodExceptionUtil.isNull(kingHoodPrincipal.getAudience(),"token信息异常");
        UserDTO user = new UserDTO();
        user.setIssuer(kingHoodPrincipal.getIssuer());
        user.setId(new Long(kingHoodPrincipal.getAudience()));
        user.setUsername(kingHoodPrincipal.getSubject());

        UserBaseDO userBaseDO = userBaseService.getUserBaseDO(user.getUsername());

        user.setRealName(userBaseDO.getRealName());

        // 获取账号权限范围
        AuthByUserDTO auth = oauthManager.getAuth(user.getIssuer(),user.getId());
        user.setAuthority(auth);

        return user;
    }


    @Override
    public UserResDTO getUser(GetUserReqDTO in) {

        boolean check = tokenProvider.validateToken(in.getToken());
        if(!check){
            return null;
        }

        Authentication authentication = tokenProvider.getAuthentication(in.getToken());
        Object principal = authentication.getPrincipal();
        if (principal instanceof KingHoodPrincipal) {
            KingHoodPrincipal kingHoodPrincipal = (KingHoodPrincipal) principal;
            KingHoodExceptionUtil.isNull(kingHoodPrincipal.getAudience(),"token信息异常");
            UserResDTO userResDTO = new UserResDTO();
            UserDTO user = new UserDTO();
            user.setIssuer(kingHoodPrincipal.getIssuer());
            user.setId(new Long(kingHoodPrincipal.getAudience()));
            user.setUsername(kingHoodPrincipal.getSubject());
            userResDTO.setUser(user);

            // 获取账号权限范围
            AuthByUserDTO auth = oauthManager.getAuth(user.getIssuer(),user.getId());
            userResDTO.setAuthority(auth);
            return userResDTO;
        }

        return null;
    }
}
